<?php

	// +----------------------------------------------------------------------
	// | 腾讯短信
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace addon\qcloud_sms;

	use Qcloud\Sms\SmsMultiSender;
	use think\Event;
	use think\Exception;
	use yicmf\addon\controller\Controller;
	use Qcloud\Sms\SmsSingleSender;
    use yicmf\Alisms;

    /**
	 * 腾讯短信
	 * @author 微尘 <yicmf@qq.com>
	 */
	class QcloudSms extends Controller
	{

        protected $cc = [

            [
                'name' => 'template',
                'title' => '短信模板',
                'type' => 'array',
                'content' => [],
                'value' => [
                    'register' => 'SMS_180775224',
                    'resetpwd' => 'SMS_180775223',
                    'forgetPwd' => 'SMS_180775223',
                    'login' => 'SMS_180775226',
                    'third_bind' => 'SMS_180775227',
                    'cashPwd' => 'SMS_204875063',
                ],
                'rule' => 'required',
                'msg' => '',
                'tip' => '',
                'ok' => '',
                'extend' => '',
            ]
        ];
		public function install()
		{
			return true;
		}

		public function uninstall()
		{
			return true;
		}
		public function subscribe(Event $event)
		{
			$event->listen('send_sms', [$this,'sendSms']);
		}
		public function sendSms($param)
		{
			try {

                $template = [
                    'edit_mobile' => 'SMS_475000798',
                    'register' => 'SMS_180775224',
                    'resetpwd' => 'SMS_180775223',
                    'forgetPwd' => 'SMS_180775223',
                    'login' => 'SMS_180775226',
                    'third_bind' => 'SMS_180775227',
                    'cashPwd' => 'SMS_204875063',
                ];
                $alisms = new Alisms();
                $result = $alisms->mobile($param['mobile'])
                    ->template($template[$param['data']['type']])
                    ->param(['code' => $param['data']['code']])
                    ->send();
                if ($result)
                {
                    $data['code'] = 0;
					$data['out_id'] = $alisms->info['RequestId'];
					$data['channel'] = 'aliyun';
                }else{
                    throw new Exception($alisms->error);
                }
			} catch (Exception $e) {
				// 备用短信渠道
				$data['code'] = 1;
				$data['message'] = $e->getMessage();
			}
			return $data;
		}

		/**
		 * 群发短信
		 * @param $param
		 * @return mixed
		 * @author 微尘 <yicmf@qq.com>
		 * @datetime: 2020/2/27 15:08
		 */
		public function sendsMultiSms($param)
		{
			try {
				$sender = new SmsMultiSender($this->getConfig('appid'), $this->getConfig('app_key'));
				if ('send' == $param['type']) {
					$templateId = 543539;
				} else {
					$templateId = 543541;
				}
				$result = $sender->sendWithParam('86', $param['mobile'], $templateId, [], '蘑菇算力');
				$result = json_decode($result, true);
				if (0 == $result['result']) {
					// 设置渠道成功
					$data['code'] = 0;
					$data['message'] = '短信发送成功';
				} else {
					$data['code'] = $result['result'];
					$data['message'] = $result['errmsg'];
				}
			} catch (Exception $e) {
				// 备用短信渠道
				$data['code'] = 1;
				$data['message'] = $e->getMessage();
			}
			return $data;
		}
	}
